<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label="$t('systemManagement.project')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'projectCode', validatorRules.projectCode]" :placeholder="$t('base.pleaseChoose')" @change="handleChangeProjectCode">
            <a-select-option v-for="item in projectList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :label="$t('light.belongingLightPole')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-model="lightPoleCode" v-decorator="[ 'lightPoleCode', validatorRules.lightPoleCode]" :placeholder="$t('base.pleaseChoose')">
            <a-select-option v-for="item in lightPoleList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :label="$t('base.code')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'code', validatorRules.code]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label="$t('base.name')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'name', validatorRules.name]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label="$t('radio.materialCode')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['materialCode', validatorRules.materialCode]" :triggerChange="true" :placeholder="$t('base.pleaseChoose')"
                              dictCode="glr_wcamera"/>
        </a-form-item>

        <a-form-item :label="$t('video.equipmentModel')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'equipmentModel', validatorRules.equipmentModel]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label="$t('video.equipmentSerial')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'equipmentSerial', validatorRules.equipmentSerial]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label="$t('video.equipmentChannel')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'equipmentChannel', validatorRules.equipmentChannel]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label=" $t('video.equipmentUploadCode')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'equipmentUploadCode', validatorRules.equipmentUploadCode]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label=" $t('video.equipmentStyleType')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['equipmentStyleType', validatorRules.equipmentStyleType]" :triggerChange="true" :placeholder="$t('base.pleaseChoose')"
                              dictCode="glr_video_equipment_style_type"/>
        </a-form-item>

        <a-form-item :label=" $t('video.equipmentFunctionType')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['equipmentFunctionType', validatorRules.equipmentFunctionType]" :triggerChange="true" :placeholder="$t('base.pleaseChoose')"
                              dictCode="glr_video_equipment_function_type"/>
        </a-form-item>

        <a-form-item :label=" $t('video.belongingYS')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            mode="false"
            style="width: 100%;height: 25px;"
            :placeholder="$t('base.pleaseChoose')"
            optionFilterProp = "children"
            v-decorator="['videoYsCode',validatorRules.videoYsCode]">
            <a-select-option v-for="(glrVideoYs,glrVideoYsIndex) in glrVideoYsList" :key="glrVideoYsIndex.toString()" :value="glrVideoYs.code">
              {{ glrVideoYs.name }}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :label=" $t('video.ytIp')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'ytIp', validatorRules.ytIp]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label=" $t('video.ytUsername')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'ytUsername', validatorRules.ytUsername]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label=" $t('video.ytPassword')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'ytPassword', validatorRules.ytPassword]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label=" $t('video.equipmentMobile')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'equipmentMobile', validatorRules.equipmentMobile]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label=" $t('video.equipmentConnection')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['equipmentConnection', validatorRules.equipmentConnection]" :triggerChange="true" :placeholder="$t('base.pleaseChoose')"
                              dictCode="glr_video_equipment_connection"/>
        </a-form-item>

        <a-form-item :label=" $t('video.equipmentVerification')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'equipmentVerification', validatorRules.equipmentVerification]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction,getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: "GlrVideoModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:this.$t('base.operation'),
        width:800,
        visible: false,
        lightPoleCode:'',
        projectList:[],
        lightPoleList:[],
        glrVideoYsList:[],
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: {rules: [{required: true, message: this.$t('base.pleaseEnter')+'!'},]},
          name: {rules: [{required: true, message: this.$t('base.pleaseEnter')+'!'},]},
          sysCompanyCode: {rules: []},
          materialCode: {rules: []},
          lightPoleCode: {rules: [{required: true, message: this.$t('base.pleaseChoose')+'!'},]},
          projectCode: {rules: [{required: true, message: this.$t('base.pleaseChoose')+'!'},]},
          equipmentSerial: {rules: [{required: true, message: this.$t('base.pleaseEnter')+'!'},]},
          equipmentChannel: {rules: [{required: true, message: this.$t('base.pleaseEnter')+'!'},]},
          videoYsCode: {rules: [{required: true, message: this.$t('base.pleaseChoose')+'!'},]},
          equipmentVerification: {rules: [{required: true, message: this.$t('base.pleaseEnter')+'!'},]},
        },
        url: {
          add: "/glr/glrVideo/add",
          edit: "/glr/glrVideo/edit",
          getGlrVideoYs: "/glr/glrVideoYs/getGlrVideoYs",
          getLightPoleList: "/glr/glrLightPole/getLightPoleList",
          getProjectList: "/glr/glrProject/getProject",
        }
      }
    },
    created () {

    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        const that = this
        getAction(this.url.getGlrVideoYs,null).then((res)=>{
          if(res.success){
            that.glrVideoYsList = res.result;
            that.$forceUpdate();
          }
        })

        httpAction(that.url.getProjectList,null,"post").then((res)=>{
          if(res.success){
            that.projectList=res.result;
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
        that.lightPoleList=[];
        that.model={};
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'code','name','videoYsCode','equipmentModel','equipmentSerial','equipmentStyleType','equipmentFunctionType','equipmentUploadCode','equipmentChannel',
            'projectCode','lightPoleCode','materialCode',
          'ytIp','ytUsername','ytPassword','equipmentMobile','equipmentConnection','equipmentVerification',
          'sysCompanyCode'))
        })
        if (this.model.projectCode){
          this.handleChangeProjectCode(this.model.projectCode);
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                //that.$message.success(res.message);
                that.$message.success(that.$t('base.operationSuccessful'));
                that.$emit('ok');
              }else{
                //that.$message.warning(res.message);
                that.$message.warning(that.$t('base.operationFailed'));
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'code','name','videoYsCode','equipmentModel','equipmentSerial','equipmentStyleType','equipmentFunctionType','equipmentUploadCode','equipmentChannel',
          'projectCode','lightPoleCode','materialCode',
          'ytIp','ytUsername','ytPassword','equipmentMobile','equipmentConnection','equipmentVerification',
          'sysCompanyCode'))
      },
      handleChangeProjectCode(projectCode){
        const that = this;
        httpAction(that.url.getLightPoleList,{projectCode:projectCode,isWisdom:'Y'},"post").then((res)=>{
          if(res.success){
            that.lightPoleList=res.result;
            that.lightPoleList.unshift({id: "0000",code: "",name: that.$t('base.pleaseChoose')+'!'});
            if (that.lightPoleList.length>0){
              //that.form.setFieldsValue({'lightPoleCode': that.lightPoleList[0].code});
            }
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      }
    }
  }
</script>